package com.example.vaadin;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Properties;
import net.sf.saxon.Configuration;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
import net.sf.saxon.trans.XPathException;


public class GraphsParser {

    public static final String xmlPath = "/home/norexan/git/weather-in-cz/weather-in-cz-backend/xmls/";
    public static final String xslPath = "/home/norexan/git/weather-in-cz/weather-in-cz-googlemaps/xml-to-html-month.xsl";
    public static final String outputPathForXML = "/test/result.html";
    public static final String outputPathForXQ = "/test/xquery.html";
            
    public static final String xqPath = "/home/norexan/git/weather-in-cz/weather-in-cz-graphs/weather.xq";
    
    public static String getPathToHTML(String pathToXML, String pathToXSL, String outputPath, Integer month) throws Exception {
        StreamSource xml = new StreamSource(new File(pathToXML));
        StreamSource xslt = new StreamSource(new File(pathToXSL));
    
        Transformer transformer = TransformerFactory.newInstance().newTransformer(xslt);
        transformer.setParameter("month", new Integer(month));

        File file = new File(outputPath);
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        transformer.transform(xml, new StreamResult(bw));
        bw.close();
        fw.close();
        return outputPath;
    }
    
    public static String getPathToGraph(String pahtToXML, String pathToXQuery, String outputPath, String year, String region, String month, String url) throws Exception {
        Configuration config = new Configuration();
        StaticQueryContext staticContext = config.newStaticQueryContext();
        XQueryExpression exp = staticContext.compileQuery(new FileReader(pathToXQuery));
        
        DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        dynamicContext.setParameter("year", year);
        dynamicContext.setParameter("url", url);
        dynamicContext.setParameter("month", month);
        dynamicContext.setParameter("region", region);
        
        Properties props = new Properties();
        props.setProperty(OutputKeys.METHOD, "xml");
        props.setProperty(OutputKeys.INDENT, "yes");
              
        File file = new File(outputPath);
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        
        exp.run(dynamicContext, new StreamResult(bw), props);
        fw.close();
        bw.close();
        return outputPath;
    }
    
    
}